import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsData:[],
    cartData:[],
    totalMount:0,
    totalPrice:0,
    totalSelect:false,
    type:0,
    status:'all',
    deleteShow:true
  },
  mutations: {
    //1.  请求商品列表
    SET_DATA(state,data){
      const {goodsData}=data;
      state.goodsData=goodsData;
     
    },

    //5. 请求购物车列表
    SET_CARTDATA(state,data){
      
      const {cartData,totalMount,totalPrice}=data;
      state.cartData=cartData;
      state.totalMount=totalMount;
      state.totalPrice=totalPrice;
    },

    //2. 修改tab切换的type类型
    SET_TYPE(state,type){
      state.type=type;
    },
    //3. 修改排序的状态
    SET_STATUS(state,status){
      state.status=status
    },

    // 4. 加入购物车
    SET_CART(state,data){
      const {id,img,name,price,stock,_type}=data;
      const index = state.cartData.findIndex(item=>item.id==id);

      if(index==-1){
        state.cartData.push({
          id,img,name,price,stock,_type,
          mount:1,
          tPrice:price
        })
      }else{
        let item=state.cartData[index];
        if(_type=='PLUS'){

          item.mount+=1;
          item.tPrice+=price;
        }else if(_type=='REDUCE'){
          // item.mount-=1;
          // item.tPrice-=price;

          if(item.mount>1){
            item.mount-=1;
            item.tPrice-=price;
          }else{
            alert("数量不能再减少了")
          }

          // item.mount-=1;
          // item.tPrice-=price;

          // if(!item.mount){
          //   state.cartData.splice(index,1);
          //   // state.cartData=state.cartData.filter(item=>item.id!=id)
          // }

        }
      }

      localStorage.setItem('cartData',JSON.stringify(state.cartData));
    },

    SET_TOTAL(state,data){
      const {select,mount,tPrice}=data;
      if(select){
        state.totalMount+=mount;
        state.totalPrice+=tPrice;
      }else{
        state.totalMount-=mount;
        state.totalPrice-=tPrice;
      }

      
      localStorage.setItem('totalMount',JSON.stringify(state.totalMount));
      localStorage.setItem('totalPrice',JSON.stringify(state.totalPrice));

    },

    //8. 修改选中的状态
    SET_SELECT(state,id){
      // state.cartData.map(item=>{
      //   if(item.id==id){
      //     item.select=!item.select
      //   }
      // })

      const index=state.cartData.findIndex(item=>item.id==id);
      state.cartData[index].select=!state.cartData[index].select

      localStorage.setItem('cartData',JSON.stringify(state.cartData));
    },

    //修改全选的状态
    SET_TOTAL_SELECT(state,boo){
      state.totalSelect=boo;
      if(boo){
        state.totalMount=0;
        state.totalPrice=0;
        state.cartData.map(item=>{
          item.select=true;
          state.totalMount+=item.mount;
          state.totalPrice+=item.tPrice;
        })
      }else{
        state.cartData.map(item=>{
          item.select=false;
          state.totalMount=0;
          state.totalPrice=0;
        })
      }

      localStorage.setItem('cartData',JSON.stringify(state.cartData));
      localStorage.setItem('totalMount',JSON.stringify(state.totalMount));
      localStorage.setItem('totalPrice',JSON.stringify(state.totalPrice));
      localStorage.setItem('totalSelect',JSON.stringify(state.totalSelect));

    },

    SET_DELETE_SHOW(state,boo){
      state.deleteShow=boo;
    }
  },
  actions: {
    setData({commit}){
      // console.log(context);
      // context.commit('SET_DATA')
      // const goodsData=localStorage.getItem('goods')?JSON.parse(localStorage.getItem('goods')):[];
      const goodsData=JSON.parse(localStorage.getItem('goods'))||[];
     
      commit('SET_DATA',{
        goodsData
      })
    },
    setCartData({commit}){
      const cartData=JSON.parse(localStorage.getItem('cartData'))||[];
      const totalMount=JSON.parse(localStorage.getItem('totalMount'))||0;
      const totalPrice=JSON.parse(localStorage.getItem('totalPrice'))||0;
      const totalSelect=JSON.parse(localStorage.getItem('totalSelect'))||false;
      commit('SET_CARTDATA',{
        cartData,
        totalMount,
        totalPrice,
        totalSelect
      })
    },

    setType({commit},type){
      commit('SET_TYPE',type)
    },
    setStatus({commit},type){
      commit('SET_STATUS',type)
    },

    setCart({commit},data){
      commit('SET_CART',data)
    },
    // 9
    setTotal({commit},data){
      commit('SET_TOTAL',data);
    },

    // 8. 
    setSelect({commit},id){
      commit('SET_SELECT',id);
    },
    setTotalSelect({commit},boo){
      commit('SET_TOTAL_SELECT',boo);
    }
    
  },
  modules: {
  }
})
